package com.example.community.util;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Component
public class MailClient {
    private static final Logger logger = LoggerFactory.getLogger(MailClient.class);
    @Autowired
    JavaMailSenderImpl mailSender;
    @Value("${spring.mail.username}")
    private String from;

    public void sendMail(String to,String subject,String content) {
        try {
            //邮件设置2：一个复杂的邮件
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            //开启多文件
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setSubject(subject);
            helper.setText(content,true);
            helper.setTo(to);
            helper.setFrom(from);
            //发送附件
            //helper.addAttachment("1.png",new File("C:/Users/Administrator/Desktop/1.png"));
            mailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
            logger.error("发送邮件失败"+e.getMessage());
        }
    }
}

